/*
 * File: E:\Kan\ARM\Projects\Google Code\Umrichter\trunk\SimulinkModelle\slprj\ert\_sharedutils\look1_1fbcpvw2bqal.c
 *
 * Code generated for Simulink model 'Fu_Dummy_20ms'.
 *
 * Model version                  : 1.314
 * Simulink Coder version         : 8.2 (R2012a) 29-Dec-2011
 * TLC version                    : 8.2 (Dec 29 2011)
 * C/C++ source code generated on : Sun Apr 28 20:08:47 2013
 */

#include "rtwtypes.h"
#include "rtw_shared_utils.h"

int16_T look1_1fbcpvw2bqal(uint16_T u0, const uint16_T bp0[], const int16_T
  table[], uint32_T prevIndex[], uint32_T maxIndex)
{
  uint32_T frac;
  uint32_T startIndex;
  int16_T intermRes;
  uint32_T iRght;
  uint32_T found;

  /* Lookup 1-D
     Canonical function name: look1_iu16lu32n31ts16Ds32_pbinlcs
     Search method: 'binary'
     Use previous index: 'on'
     Interpolation method: 'Linear'
     Extrapolation method: 'Clip'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
     Rounding mode: 'simplest'
   */
  /* Prelookup - Index and Fraction
     Index Search method: 'binary'
     Extrapolation method: 'Clip'
     Use previous index: 'on'
     Use last breakpoint for index at or above upper limit: 'off'
     Remove protection against out-of-range input in generated code: 'off'
     Rounding mode: 'simplest'
   */
  if (u0 <= bp0[0U]) {
    startIndex = 0U;
    frac = 0U;
  } else if (u0 < bp0[maxIndex]) {
    startIndex = prevIndex[0U];

    /* Binary Search using Previous Index */
    frac = 0U;
    iRght = maxIndex;
    found = 0U;
    while (found == 0U) {
      if (u0 < bp0[startIndex]) {
        iRght = startIndex - 1U;
        startIndex = (iRght + frac) >> 1U;
      } else if (u0 < bp0[startIndex + 1U]) {
        found = 1U;
      } else {
        frac = startIndex + 1U;
        startIndex = (iRght + frac) >> 1U;
      }
    }

    frac = div_repeat_u32((uint32_T)(uint16_T)((uint32_T)u0 - (uint32_T)
      bp0[startIndex]) << 16, (uint32_T)(uint16_T)((uint32_T)bp0[startIndex + 1U]
      - (uint32_T)bp0[startIndex]), 15U);
  } else {
    startIndex = maxIndex - 1U;
    frac = 2147483648U;
  }

  prevIndex[0U] = startIndex;

  /* Interpolation 1-D
     Interpolation method: 'Linear'
     Use last breakpoint for index at or above upper limit: 'off'
     Rounding mode: 'simplest'
     Overflow mode: 'wrapping'
   */
  intermRes = (int16_T)mul_s32_s32_u32_sr31(table[startIndex + 1U] -
    table[startIndex], frac);
  return (int16_T)(table[startIndex] + intermRes);
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
